#include<iostream>
#include<string>
#include<unistd.h>

using namespace std;
std::string Sum(const std::string &message)
{
    std::string str1,str2;
    std::string type1,type2;
    std::string value1,value2;
    size_t index = message.find("&");
    if(index == message.npos) cerr << "message find is error" << endl;
    str1 = message.substr(0,index);
    str2 = message.substr(index+1);
    index = str1.find("=");
    if(index == str1.npos) cerr << "str1 find is error" << endl;
    type1 = str1.substr(0,index);
    value1 = str1.substr(index+1);
    index = str2.find("=");
    if(index == str2.npos) cerr << "str2 find is error" << endl;
    type2 = str2.substr(0,index);
    value2 = str2.substr(index+1);

    int value = atoi(value1.c_str()) + atoi(value2.c_str());
    std::string ret = type1;
    ret += "+";
    ret += type2;
    ret += "=";
    ret += to_string(value);
    cerr << ret << endl;
    
    return ret;
}
void SendHttp(const std::string& ret)
{
  std::string html;
  html += "<!DOCTYPE html>\
<html lang=\"en\">\
<head>\
    <meta charset=\"UTF-8\">\
    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\
    <title>Document</title>\
</head>\
<body>\
    <p>";html += ret;html += "</p>\
</body>\
</html>";

  cout << html;
}
int main()
{
  cerr << "-------------------------" << endl;
  std::string str = getenv("METHOD");
  cerr << str << endl;
  if(str == "GET")
  {
    std::string message = getenv("MESSAGE");
    cerr << message << endl;
    SendHttp(Sum(message));
  }
  else if(str == "POST")
  {
    std::string lenstr = getenv("CONTENT_LENGTH");
    cerr << lenstr << endl;
    int len = atoi(lenstr.c_str());
    std::string body;
    char ch;
    while(len > 0)
    {
      read(0,&ch,1);
      body += ch;
      --len;
    }
    cerr << body << endl;
    SendHttp(Sum(body));
  }

  return 0;
}
